// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'

/*
 * 使用el-ui
 */
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 配置国际化
import DatePicker from 'element-ui/lib/date-picker'
import enLocale from 'element-ui/lib/locale/lang/en'
import zhLocale from 'element-ui/lib/locale/lang/zh-CN'
import ElementLocale from 'element-ui/lib/locale'

import router from './router'
import Store from './store/index.js'
import ACLUserRepo from './api/ACLUserRepo.js'
import RequestApi from './api/RequestApi.js'

import VueI18n from 'vue-i18n'
// import language js
import zh from './language/zh.js'
import en from './language/en.js'
// create pdf
import PDFUtils from './utils/jspdf.js'
// date
import DateUtils from './utils/DateUtils.js'
// string 工具类
import StringUtils from './utils/StringUtils.js'
// 消息工具类
import MessageUtils from './utils/MessageUtils.js'
// 加密工具类
import JSEncryptUtils from './utils/JSEncryptUtils.js'
// 子页导航显示
import NavigationsUtils from './utils/Navigations.js'
// 全局 日志
import Log from './utils/LogUtils.js'
// set global css
import './assets/css/global.css'
// icon css
// import '../static/icon/iconfont.css'
import LanguageConfig from './assets/js/languageConfig.js'
// 组件
import leftTree from './components/intermediate/component/LeftTree.vue'
import Navigations from './components/intermediate/component/Navigations.vue'
// 引用NProgress 加载视图
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
// 动态修改title
import VueWechatTitle from 'vue-wechat-title'

Vue.prototype.DateUtils = DateUtils
Vue.prototype.StringUtils = StringUtils
Vue.prototype.MessageUtils = MessageUtils
Vue.prototype.JSEncryptUtils = JSEncryptUtils
Vue.prototype.Store = Store
Vue.prototype.ACLUserRepo = ACLUserRepo
Vue.prototype.PDFUtils = PDFUtils
Vue.prototype.RequestApi = RequestApi
Vue.prototype.NavigationsUtils = NavigationsUtils

Vue.config.productionTip = false
Vue.use(ElementUI, { size: 'small', zIndex: 3000 })
Vue.use(NProgress)
Vue.use(VueI18n)
Vue.use(DatePicker)
Vue.use(router)
Vue.use(Log)
Vue.use(VueWechatTitle)
Vue.component('Left-Tree', leftTree)
Vue.component('Navigations', Navigations)
/**
 * 多语言设置
 */
Vue.prototype.setLanguage = function (languageObj) {
  if (localStorage.getItem('language') !== languageObj) {
    localStorage.setItem('language', languageObj)
    this.$i18n.locale = languageObj
  }
}

const messages = {
  en: {
    ...en,
    ...enLocale
  },
  zh: {
    ...zh,
    ...zhLocale
  }
}
ElementLocale.i18n((key, value) => i18n.t(key, value))
if (localStorage.getItem('language') == null) {
  localStorage.setItem('language', 'zh')
}
const i18n = new VueI18n({
  locale: localStorage.getItem('language') || 'zh', // set locale
  messages, // set locale messages
  silentTranslationWarn: false // show status
})

/**
 * 路由守卫
 */
router.beforeEach((to, form, next) => {
  NProgress.start()
  console.log(to, form)
  if (StringUtils.isNotEmpty(Store.state.token)) {
    let roles = Store.getters.getRoles && Store.getters.getRoles.length > 0
    let routerData = Store.getters.addRouter && Store.getters.addRouter.length > 0
    let navigations = Store.state.navigations && Store.state.navigations.length > 0
    if ((!roles) || (!routerData) || (!navigations)) {
      ACLUserRepo.getRoleByUser()
      ACLUserRepo.getLeftTreeListUser()
      // 路由数据 obj
      Store.dispatch('generateRoutes', Store.state.navigations).then(() => {
        // 动态添加路由
        router.addRoutes(Store.getters.addRouter)
        // 确保路由添加完成
        next({ ...to, replace: true })
      })
    } else {
      next()
    }
  } else {
    if (StringUtils.verifyTheSame(to.path, form.path)) {
      next()
    } else {
      if (StringUtils.verifyTheSame(to.path, '/')) {
        next()
      } else {
        next({
          name: 'login'
        })
      }
    }
  }
  NProgress.done()
})

// 可配置语言项
Store.state.languageConfig = LanguageConfig.languageConfig
/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  i18n,
  components: {
    App
  },
  template: '<App/>',
  created: function () {
    let that = this
    let navigations = Store.state.navigations && Store.state.navigations.length > 0
    if (navigations) {
      Store.dispatch('generateRoutes', Store.state.navigations).then(() => {
        router.addRoutes(Store.getters.addRouter)
      })
    }
    if (!Store.state.configSettingStatus) {
      ACLUserRepo.getConfig().then(function (response) {
        Store.state.configSetting = response.data
        Store.state.configSettingStatus = true
      }).catch(function () {
        MessageUtils.notifyError(that, '服务基础配置加载失败!')
      })
    }
  }
})
